<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<jsp:include page="../inc.jsp"></jsp:include>
<script type="text/javascript">
	var incomedatagrid;
	$(function() {
		incomedatagrid = $('#income-datagrid').treegrid(
				{
					url : "${pageContext.request.contextPath}/IncomeController/getIncomeTreeGird",
					rownumbers : true,
					fit : true, //自动大小
					fitColumns : false, //自适应列宽
					striped : true,
					idField : 'pid',
					treeField : 'pid',
					columns : [ [
							{
								title : '项目信息',
								field : 'pid',
								width : 300,
								formatter : function(value, row, index) {
									if (value == "2013" || value == "2014" || value == "2012" || value == "2015"
											|| value == "2016")
										return value;
									else if (row.incomeName == null) {
										return row.departmentName;
									} else {
										return row.incomeName;
									}
								}
							}, {
								title : '项目收入',
								field : 'incomeValue',
								width : 100,
								formatter : function(value, row, index) {
									//判断当前展示的是否为收支表，如果不是则遍历ROW的子节点，获取此部门下的所有收支表的收入，并合计
									if (checkIsCensus(row)) {
										value = recursion(row, "incomeValue");
										return (accounting.formatNumber(value));
									} else {
										if (value > 0) {
											return (accounting.formatNumber(value));
										} else {
											return "";
										}
									}

								}
							}, {
								title : '合同金额',
								field : 'contractValue',
								width : 100,
								formatter : function(value, row, index) {
									//判断当前展示的是否为收支表，如果不是则遍历ROW的子节点，获取此部门下的所有收支表的收入，并合计
									if (checkIsCensus(row)) {
										value = recursion(row, "contractValue");
										return (accounting.formatNumber(value));
									} else {
										if (value > 0) {
											return (accounting.formatNumber(value));
										} else {
											return "";
										}
									}
								}
							}, {
								title : '回款金额',
								field : 'billValue',
								width : 100,
								formatter : function(value, row, index) {

									//判断当前展示的是否为收支表，如果不是则遍历ROW的子节点，获取此部门下的所有收支表的收入，并合计
									if (checkIsCensus(row)) {
										value = recursion(row, "billValue");
										return (accounting.formatNumber(value));
									} else {
										if (value > 0) {
											return (accounting.formatNumber(value));
										} else {
											return "";
										}
									}
								}
							}, {
								title : '合同收入偏差',
								field : 'Deviation',
								width : 100,
								formatter : function(value, row, index) {
									//判断当前展示的是否为收支表，如果不是则遍历ROW的子节点，获取此部门下的所有收支表的合同-收入
									var incomeValue;
									var contractValue
									if (checkIsCensus(row)) {
										incomeValue = recursion(row, "incomeValue");
										contractValue = recursion(row, "contractValue");
										return accounting.formatNumber(contractValue - incomeValue);
									} else {
										if (row.incomeName == null) {
											return "";
										} else {

											return accounting.formatNumber(row.contractValue - row.incomeValue);
										}
									}
								}
							}, {
								title : '应收款',
								field : 'receivableValue',
								width : 100,
								formatter : function(value, row, index) {

									//判断当前展示的是否为收支表，如果不是则遍历ROW的子节点，获取此部门下的所有收支表的合同-收入
									var billValue;
									var contractValue
									if (checkIsCensus(row)) {
										billValue = recursion(row, "billValue");
										incomeValue = recursion(row, "incomeValue");
										return accounting.formatNumber(billValue - incomeValue);
									} else {
										if (row.incomeName == null) {
											return "";
										} else {

											return accounting.formatNumber(row.billValue - row.incomeValue);
										}
									}
								}
							} ] ],
					onDblClickRow : function(row)//双击事件，双击后弹出收入详细页面
					{
						if (row.incomeName == null) {
							return;
						}
						var urlstr = '${pageContext.request.contextPath}/IncomeController/incomeDetail?incomeName='
								+ row.incomeName;
						urlstr = urlstr.replace(/%/g, "%25");
						urlstr = urlstr.replace(/\&/g, "%26");
						urlstr = urlstr.replace(/\+/g, "%2B");

						$('#incomeDetail').dialog({
							title : '收入详细页面',
							width : '550px',
							height : '550px',
							maximizable : false,
							minimizable : false,
							collapsible : false,
							href : urlstr,
							modal : true
						});
					},
					onLoadSuccess : function(row, data) {
						$('#income-datagrid').treegrid('collapseAll');
					}
				});

	});

	function recursion(row, censusName) {
		var result = 0;
		var children = row.children;
		if (row.lowestBranch == "true") {
			var incomeList = children;
			if (incomeList != null && incomeList != undefined) {
				for (var i = 0; i < incomeList.length; i++) {
					var array = incomeList[i];
					if (censusName == "incomeValue") {
						result = result + array.incomeValue;
					} else if (censusName == "contractValue") {
						result = result + array.contractValue;
					} else if (censusName == "billValue") {
						result = result + array.billValue;
					}
				}
				return result;
			}

		} else {
			if (children != null && children != undefined)
				for (var i = 0; i < children.length; i++) {
					result = result + recursion(children[i], censusName);
				}
		}
		return result;
	}

	/**
	 * 判断是否需要统计展示
	 */
	function checkIsCensus(row) {

		var owners = row.owners;
		//1:判断当前是否为收支表，如果是，则true
		if (row.incomeName == null) {
			//2:判断当前用户是否为系统管理员
			var logUserName = "${session.employeeName}";
			if (logUserName == "admin") {
				return true;
			}
			//3:判断是否为年份目录，如果为年份目录，则展示
			if (row.pid == "2013" || row.pid == "2014" || row.pid == "2012" || row.pid == "2015" || row.pid == "2016") {
				return true;
			}
			//4:2，3条不满足，则说明当前的节点不为年份节点，且登录的用户不为admin，则要判断此节点用户是否有管理权限
			if (owners.indexOf(logUserName) > 0) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	function incomeDatagridReload() {
		$('#income-datagrid').treegrid('reload');
	}
</script>
</head>
<body>
	<div id="p" class="easyui-panel" title="项目收入信息"
		style="height: 500px; padding: 0px; background: #fafafa;"
		data-options="fit:true,tools : [ {
						iconCls : 'icon-reload',
						handler : function() {
							incomeDatagridReload();
						}
					} ]">
		<table id="income-datagrid"></table>

	</div>

</body>
<div id="incomeDetail"></div>
</html>

